Check SMTP connection from Linux Command Line

Introduction

SMTP connection errors could be resolved in numerous ways but command line detects the error from the root quite easily. In this conundrum, Telnet is widely used to test SMTP connection breaks. Testing an email server with Telnet client is where we would focus, going forward with our solution. In this article we would be solving one of the most widely searched issues and a potential SMTP connection pain point for the developers. To check if SMTP is working from the command line (Linux), is one critical aspect to be considered while setting up an email server.

The most common way of checking SMTP from Command Line is using telnetopenssl or ncat (nc) command. It is also the most prominent way to test SMTP Relay. Let's see how it works. 

Note: these commands are Linux specific, so won't work in other environments. 

SMTP From Command Line - Some Important Commands


1. Telnet:
 Used to communicate with another host using the telnet protocol. If the hostname is not provided it enters into command mode.

 

Note: If telnet is not available, copy and paste the command

sudo yum install telnet

2. ncat or nc: It's a general purpose CLI tool used for port scanning, security tool, and monitoring tool.
Note: If ncat or nc is not available, copy and paste the below command

sudo yum install nmap-ncat -y

3. openssl: It's a general purpose library that provides an open-source implementation of SSL and TLS protocol.
Note: If openssl is not installed, copy and paste the below command:

sudo yum install openssl

Step 1: Checking SMTP Connection Using Telnet

Telnet to your host, you want to check connection. Smtp servers usually communicate over smtp ports 25, 2525,587. Telnet SMTP test is one the best ways to figure out your SMTP connection state. 

Did you know that SMTP Port 25 is the default SMTP Port? Read in detail.

Syntax : telnet [your hostname ] [port no] 

telnet smtp.pepipost.com 25 [2525 | 587 ] 

In order  to quit telnet ctrl + ] and quit

Output: If it's getting connected properly 

 

You can even try sending email using telnet.

Output: If the connection is refused.

Step 2: Checking The SMTP Connection From Command Line Using Ncat Or Nc

Because ncat or nc has various functionality from the list of functionality we will just check the connection using the following command.

nc -v -u smtp.example.com 587

If it is successfully connected

[root@nl587 auth]# ncat -v -u smtp.pepipost.com 587

Ncat: Version 7.50 ( https://nmap.org/ncat )

Ncat: Connected to 138.197.56.58:587.

If there is a problem connecting the host 

[root@nl587 auth]# ncat -v -u smtp.example.com 587

Ncat: Version 7.50 ( https://nmap.org/ncat )

Ncat: Could not resolve hostname "smtp.example.com": Name or service not known. QUITTING.

All of the above steps are used to check the SMTP connection from command line to a particular port of the host. The next step is used to communicate through a secure tunnel using TLS. TLS security is of paramount importance when it comes to email deliverability. Read our detailed blog on TLS Security (Highly Recommended). 

Step 3: Checking SMTP Connection Over TLS Using Openssl

In order to set up a TLS encrypted connection to an SMTP server, you need openssl command.

openssl s_client -connect smtp.pepipost.com:587 -starttls smtp 

Once you hit the above command,you see a lot of verbose.

Above all, when a connection is successfully established, then you will notice a 250 DSN.

 

Consequently, once you got 250 DSN you can start now your SMTP transaction now by using below command

EHLO “i am here”

EHLO i am here

250-smtpbny-lb2.pepipost.com

250-PIPELINING

250-SIZE 50000000

250-VRFY

250-ETRN

250-AUTH PLAIN LOGIN

250-AUTH=PLAIN LOGIN

250-ENHANCEDSTATUSCODES

250-8BITMIME

250 DSN

this will give you supported verbose.

It will not respond if your SMTP server is not active.

These are some of the popular methods to check whether the SMTP connection is active on your machine. In case you have installed the SMTP correctly, but still getting an error while sending emails, then please refer this doc for more information.